package demo.Thread;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/10-08-10-11:33
 * @Description：demo.Thread   线程安全问题
 */
/*
线程安全
    多线程同时 操作堆中的同一个对象，可能会造成数据和预期不符合   ->线程不安全
 */
public class thread_security {
    static int num = 10;
    public static void main(String[] args) {
        Thread thread1 = new Thread(){
            @Override
            public void run() {
                while(num > 0){
                    System.out.println("thread1: " + num);
                    num--;
                }
            }
        };
        Thread thread2 = new Thread(){
            @Override
            public void run() {
                while(num > 0){
                    System.out.println("thread2: " + num);
                    num--;
                }
            }
        };
        thread1.start();
        thread2.start();
        /*
            thread2: 10
            thread2: 9
            thread2: 8
            thread2: 7
            thread2: 6
            thread2: 5
            thread2: 4
            thread2: 3
            thread2: 2
            thread2: 1
            thread1: 10
         */
    }
}
